package com.czl.module_storehouse.activity.remand.damage;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import com.czl.base.data.bean.tengyun.RecordRemandBean;
import com.czl.module_base.activity.BaseRecyclerViewActivity;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_service.databinding.LayoutSearchBinding;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.DamageRecordAdapter;
import com.czl.module_storehouse.event.DamageRecordEvent;
import com.czl.module_storehouse.mvp.presenter.RemandHomePresenter;
import com.czl.module_storehouse.mvp.view.RemandHomeView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * @author xubin
 */
public class DamageRecordActivity extends BaseRecyclerViewActivity<LayoutSearchBinding> implements RemandHomeView {

    private DamageRecordAdapter mAdapter;
    @InjectPresenter
    RemandHomePresenter mRemandHomePresenter;

    @Override
    protected LayoutSearchBinding getChildBinding(LayoutInflater layoutInflater, FrameLayout containerTop) {
        return LayoutSearchBinding.inflate(layoutInflater, containerTop, true);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("损坏记录");
        mAdapter = new DamageRecordAdapter(R.layout.item_damage_record, new ArrayList<>());
        mAdapter.setOnItemClickListener((adapter, view, position) -> nextProductActivity(mAdapter.getItem(position)));
        binding.recyclerView.setAdapter(mAdapter);
        mChildBinding.etSearch.setHint(getString(R.string.enter_name_or_tem_name));
        mChildBinding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mChildBinding.ivSearch.setOnClickListener(v -> loadData(true, mChildBinding.etSearch.getText().toString()));
    }

    public void nextProductActivity(RecordRemandBean item) {
        if (item.getProductListSize() > 0) {
            EventBus.getDefault().postSticky(new DamageRecordEvent(item));
            startActivity(new Intent(getContext(), DamageRecordProductActivity.class));
        }
    }

    @Override
    protected void loadData() {
        super.loadData();
        loadData(true, "");
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        loadData(false, "");
    }

    private void loadData(boolean refresh, String name) {
        mRemandHomePresenter.getDamageSortList(refresh, name);
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        handleResponseData(mAdapter, mRemandHomePresenter.getPageNo(), (ListBean<RecordRemandBean>) t);
    }
}
